%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     pb 工具
%%% @end
%%%-------------------------------------------------------------------
-module(util_net).

-export([
    send/2
    ,send_filter/2
    ,pack_send/3

    ,pack/2
]).

-include("common.hrl").


%%%===================================================================
%%% API functions
%%%===================================================================

send(ConnPid, Bin) ->
    ConnPid ! {send, Bin}.

send_filter(ConnPid, Bin) ->
    ConnPid ! {send_filter, Bin}.

pack_send(ConnPid, Cmd, Data) ->
    case ?MODULE:pack(Cmd, Data) of
        {ok, Bin} ->
            ?MODULE:send(ConnPid, Bin);
        Error ->
            ?ERR("打包数据出错Cmd:~w,Data:~w,Err:~w", [Cmd, Data, Error])
    end.


pack(Cmd, Data) ->
    case mapping:module(game_server, Cmd) of
        {ok, _Auth, _Caller, Proto, _ModName} ->
            catch Proto:pack(Cmd, Data);
        {error, _} ->
            {error, "模块映射失败"}
    end.
